package com.zrfintech.manage.service;

import cn.hutool.crypto.digest.MD5;
import com.zrfintech.domain.MgUser;
import com.zrfintech.domain.MgUserRole;
import com.zrfintech.service.MgUserRepo;
import com.zrfintech.service.MgUserRoleRepo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service
public class MgUserService {
    @Autowired
    private MgUserRepo mgUserRepo;

    @Autowired
    private MgUserRoleRepo mgUserRoleRepo;


    @Transactional(rollbackFor = Exception.class)
    public void upsert(MgUser user, String roleCode) {
        MgUser mgUser = new MgUser();
        mgUser.setUserName(user.getUserCode());
        mgUser.setUserCode(user.getUserCode());
        mgUser.setUserPhone(user.getUserCode());
        mgUser.setUserPassword(MD5.create().digestHex(user.getUserPassword()));
        mgUser.setUserEmail(user.getUserCode());
        mgUser.setPushId(user.getUserCode());
        mgUser.setCts(new Date());
        mgUser.setUts(new Date());
        mgUserRepo.create(mgUser);
        MgUserRole role = new MgUserRole();
        role.setUserCode(user.getUserCode());
        role.setRoleCode(roleCode);
        role.setCts(new Date());
        role.setUts(new Date());
        mgUserRoleRepo.create(role);
    }

}
